package com.yang.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yang.entity.Resource;
import com.yang.model.Page;
import com.yang.service.ResourceService;
import com.yang.service.RoleService;
import com.yang.service.UserService;

/**
 * 描述：菜单相关的操作控制器
 * 版本：1.0
 * 作者： 杨建全
 * 创建时间：2016年3月23日  上午10:02:36
 */
@Controller
@RequestMapping("/menu")
public class MenuController {
	
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private ResourceService resourceService;
	
	
	
	/**
	 * 菜单设置
	 * @return
	 */
	@RequiresAuthentication
	@RequestMapping("/menuSet")
	public String menuSet(String pageNo,HttpServletRequest request){
		Page<Resource> page = resourceService.getPage(pageNo, "10");
		request.setAttribute("page",page);
		return "/menuSet/menuSet";
	}
	
	/**
	 * 添加菜单弹窗
	 * @param request
	 * @return
	 */
	@RequiresAuthentication
	@RequestMapping("/addModalView")
	public String addModalView(HttpServletRequest request){
		return "/menuSet/addModal";
	}
	
	/**
	 * 添加菜单提交
	 * @param res
	 * @return
	 */
	@RequiresAuthentication
	@ResponseBody
	@RequestMapping("/menuAddSubmit")
	public String menuSetAddSubmit(Resource res){
		res = resourceService.save(res);
		return "success";
	}
	
	/**
	 * 编辑菜单弹窗
	 * @param id		菜单id
	 * @param request
	 * @return
	 */
	@RequiresAuthentication
	@RequestMapping("/editModalView")
	public String editModalView(String id,HttpServletRequest request){
		Resource res = resourceService.findOne(id);
		if(null!=res){
			request.setAttribute("res", res);
		}
		return "/menuSet/editModal";
	}
	
	/**
	 * 修改菜单
	 * @param res	传入的菜单对象，需带id
	 * @return
	 */
	@RequiresAuthentication
	@ResponseBody
	@RequestMapping("/menuEditSubmit")
	public String menuSetEditSubmit(Resource res){
	System.out.println(res);
		resourceService.updateResource(res);
		return "success";
	}
	
	@RequiresAuthentication
	@ResponseBody
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public boolean delete(String id){
		boolean flag = resourceService.deleteResource(id);
		return flag;
	}
}
